package com.xx.jfb.tomcat;

import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

public class HttpHandle implements Runnable {

    private SocketChannel channel;

    HttpHandle(SocketChannel channel) {
        this.channel = channel;
    }

    @Override
    public void run() {
        try{
            ByteBuffer buffer = ByteBuffer.allocate(1024);
            channel.read(buffer);
            buffer.flip();
            while (buffer.hasRemaining()){
                char b = (char)(buffer.get());
                String s = String.valueOf(b);
                System.out.print(s);
            }
            ByteBuffer reBuffer = ByteBuffer.allocate(1024);
            //添加的响应头
            String response = "HTTP/1.1\n";
            //添加的响应头
            response += "Content-type:text/html\n\n";
            response += "我是MinoTomat";
            reBuffer.put(new String(response).getBytes("GBK"));
            reBuffer.flip();
            channel.write(reBuffer);
            channel.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
